package com.xhtt.modules.app.controller.inspection;

import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.inspection.service.IInsTaskStepService;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 巡检-每次任务
 * </p>
 *
 * @author xj
 * @since 2019-01-29
 */
@RestController
@RequestMapping("app/ins-task-step")
@Api(value = "巡检-任务详情", tags = "巡检-任务详情")
public class InsTaskStepController {

    @Autowired
    private IInsTaskStepService insTaskStepService;

    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "/list")
    @ApiOperation("巡检管理-综合查询列表(web/app)")
    @Login
    public R queryPageIsFinish(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        // type存在则表示是web端，不需要做权限限制
        if (params.get("type") == null) {
            List<Integer> userIds = sysUserService.queryAuth(user);
            params.put("userIds", userIds);
        } else {
            // 不存在默认设置当前班组id
            params.computeIfAbsent("teamId", x -> user.getTeamId());
        }
        PageUtils pageUtils = insTaskStepService.queryPageIsFinish(params);
        R r = new R();
        r.put(Constant.DATA, pageUtils);
        if (Optional.ofNullable(user.getTeamId()).isPresent() && user.getUserId() != Constant.SUPER_ADMIN) {
            r.put("teamId", user.getTeamId());
        }
        return r;
    }


    @GetMapping(value = "/export")
    @ApiOperation("巡检管理-综合查询导出")
    @Login
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        params.put("user", user);
        insTaskStepService.exportExcel(response, params);
    }


}
